package com.lh.jianzhioffer.专项练习.链表;

/**
 * @author: linghao
 * @since: 2023/3/7
 *
 * 给定单链表的头节点 head ，请反转链表，并返回反转后的链表的头节点
 */
public class 反转链表024 {

    public ListNode reverseList(ListNode head) {
        ListNode temp = null;
        while (head != null){
            ListNode next = head.next;
            head.next = temp;
            temp = head;
            head = next;
        }
        return temp;
    }

    public class ListNode {
      int val;
      ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  }
}
